package com.xuecheng.content.controller;

import com.xuecheng.commons.model.dto.CourseBaseDto;
import com.xuecheng.commons.model.dto.CourseDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.vo.CourseBaseVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.config.MinioTemplate;
import com.xuecheng.content.domain.CourseBase;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * <p>
 * 课程基本信息前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
public class CourseBaseController {

    @Autowired
    private CourseBaseService courseBaseService;

    //分页+条件查询
    @PostMapping("/course/list")
    public ResponseResult<CourseBaseVo> findByPage(PageRequest pageRequest, @RequestBody CourseDto dto) {
        return courseBaseService.findByPage(pageRequest, dto);
    }
    @Autowired
    private MinioTemplate minioTemplate;

    // 上传课程封面
    @PostMapping("/course/upload")
    public ResponseResult<String> upload(MultipartFile uploadFile) throws IOException {
        // 上传文件到minio
        String picurl = minioTemplate.upload(uploadFile.getInputStream(), uploadFile.getOriginalFilename());
        // 返回图片地址
        return ResponseResult.okResult(picurl);
    }
    // 课程新增
    @PostMapping("/course")
    public ResponseResult<Map> saveCourseBase(@RequestBody CourseBaseDto dto) {
        return courseBaseService.saveCourseBase(dto);
    }
    // 课程回显
    @GetMapping("/course/{courseBaseId}")
    public ResponseResult<CourseBaseVo> findById(@PathVariable("courseBaseId") Long courseBaseId) {
        // 1.查询service
        CourseBase courseBase = courseBaseService.getById(courseBaseId);
        // 2.转vo
        CourseBaseVo vo = BeanHelper.copyProperties(courseBase, CourseBaseVo.class);
        // 3.返回
        return ResponseResult.okResult(vo);
    }
    // 课程修改
    @PutMapping("/course")
    public ResponseResult<Map> updateCourseBase(@RequestBody CourseBaseDto dto){
        return courseBaseService.updateCourseBase(dto);
    }
    // 课程删除
    @DeleteMapping("/course/{courseId}")
    public ResponseResult deleteCourseBase(@PathVariable("courseId") Long courseId){

        return courseBaseService.deleteCourseBase(courseId);
    }
}
